package com.sita.tboard.hitchhike.register;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;

import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;
import com.sita.tboard.hitchhike.HitchhikeActivity;
import com.sita.tboard.hitchhike.HitchhikeConstants;
import com.sita.tboard.hitchhike.helper.HitchhikeNetworkHelper;
import com.sita.tboard.hitchhike.helper.HitchhikePersistHelper;
import com.sita.tboard.hitchhike.listeners.OnBecomeDriverListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.trinea.android.common.util.ToastUtils;

public class DriverRegisterFormFragment extends Fragment {

    @Bind(R.id.vehicle_brand)
    AutoCompleteTextView brandView;
    @Bind(R.id.vehicle_number)
    AutoCompleteTextView numberView;

    public DriverRegisterFormFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment RegDriverFragment.
     */
    public static DriverRegisterFormFragment newInstance() {
        DriverRegisterFormFragment fragment = new DriverRegisterFormFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_hitchhike_driver_register, container, false);
        ButterKnife.bind(this, v);

        return v;
    }

    @OnClick(R.id.btn_save)
    void onClickBtnSave(){
        final String brand = brandView.getText().toString();
        final String carNumber = numberView.getText().toString();
        if (TextUtils.isEmpty(brand)){
            brandView.setError(getText(R.string.please_type_vehicle_brand));
            return;
        }
        if (TextUtils.isEmpty(carNumber)){
            numberView.setError(getText(R.string.please_type_car_number));
            return;
        }
        HitchhikeNetworkHelper.INSTANCE.becomeDriver(brand, carNumber, new OnBecomeDriverListener() {
            @Override
            public void onSuccess() {
                startActivity(HitchhikeActivity.newIntent(HitchhikeConstants.TAG_DRIVER_RUNNING));
            }

            @Override
            public void onFailure(Throwable throwable) {
                ToastUtils.show(GlobalContext.getGlobalContext(), com.sita.tboard.util.TextUtils.getThrowableMessage(throwable));
            }
        });
    }
}
